package com.example.controller;

import com.example.common.Result;
import com.example.utils.FileUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @PostMapping
    public Object upload(String text, MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String newFileName = UUID.randomUUID() + FileUtils.getFileExtension(originalFilename);
        // 创建目标文件路径， 如果目录不存在，则创建目录
        File destDir = new File("D:\\Study\\Uploads\\");
        if (!destDir.exists()) {
            destDir.mkdirs();
        }
        // 创建目标文件
        File destFile = new File(destDir, newFileName);
        // 保存文件
        file.transferTo(destFile);
        return Result.success();
    }
}
